package com.kadron.item;

import java.io.Serializable;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.creator.KItemModelCreator;
import com.kadron.dao.KDiscountItemDAO;
import com.kadron.dao.KImageDAO;
import com.kadron.dao.KItemDAO;
import com.kadron.datamodel.KDataModel;
import com.kadron.datamodel.KIModelDataProvider;
import com.kadron.entity.Item;
import com.kadron.manager.KMessageManager;
import com.kadron.manager.KSelection;

@AutoCreate
@Scope(ScopeType.PAGE)
@Name("kItemListManager")
public class KItemListManager extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6230083148778042832L;
	
	private KDataModel itemList;
	private KIModelDataProvider dataProvider;
	
	@In
	private KItemDAO itemDAO;
	@In
	private KDiscountItemDAO discountItemDAO;
	@In
	private KImageDAO imageDAO;
	
	@Create
	public void init() {
		dataProvider = new KIModelDataProvider(new KItemModelCreator(itemDAO, discountItemDAO, imageDAO), itemDAO);
		itemList = new KDataModel(dataProvider);
	}

	public KDataModel getItemList() {
		return itemList;
	}
	
	public String edit(Long id) {
		if (id == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		KSelection kselection = (KSelection) Component.getInstance("kSelection");
		kselection.setItemId(id);
		return "e_item";
	}
	
	public void delete(Item item) {
		if (item == null) {
			bad(KConst.BAD_THING);
			return;
		}
		itemList.remove(item);
		itemDAO.delete(item);
	}
	
	public void ca(Item e) {
		if (e == null) {
			bad(KConst.BAD_THING);
			return;
		}
		e.setActive(!e.isActive());
		e = (Item) itemDAO.update(e);
	}
}
